package com.java.controller;

import com.java.entity.Question;
import com.java.service.QuestionService;
import com.java.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class QuestionController extends BaseController {

    @Autowired
    private QuestionService questionService;

    @PostMapping("question/insert")
    public ResultVo insert(@RequestBody Question question) {
        int rows = questionService.insert(question);
        return toVo(rows);
    }

    @PostMapping("question/delete")
    public ResultVo delete(@RequestBody String questionId) {
        if (!StringUtils.hasText(questionId)){
            throw new RuntimeException("questionId is null");
        }
        int rows = questionService.delete(Integer.valueOf(questionId));
        return toVo(rows);
    }

    @PostMapping("question/update")
    public ResultVo update(@RequestBody Question question) {
        int rows = questionService.update(question);
        return toVo(rows);
    }

    @PostMapping("question/selectOne")
    public ResultVo selectOne(@RequestBody String questionId) {
        if (!StringUtils.hasText(questionId)){
            throw new RuntimeException("questionId is null");
        }
        Question question = questionService.selectOne(Integer.valueOf(questionId));
        return ResultVo.success(question);
    }

    @PostMapping("question/selectAll")
    public ResultVo selectAll() {
        List<Question> list = questionService.selectAll();
        return ResultVo.success(list);
    }

    @PostMapping("question/selectByQuestionTypeId")
    public ResultVo selectByQuestionTypeId(@RequestBody String questionTypeId) {
        if (!StringUtils.hasText(questionTypeId)){
            throw new RuntimeException("questionTypeId is null");
        }
        List<Question> list = questionService.selectByQuestionTypeId(Integer.valueOf(questionTypeId));
        return ResultVo.success(list);
    }

}
